//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using Microsoft.Practices.SmartClient.DisconnectedAgent;
using System;
using Shp.Runtime.Contract;

namespace LitwareHR.PublicApp.WinUX.DSA.Secure.DataModelService
{
    // Generated code for the web service.
    // Use this proxy to make requests to the service when working in an application that is occasionally connected
    public partial class Agent
    {
        IRequestQueue requestQueue;

        public Agent(IRequestQueue requestQueue)
        {
            this.requestQueue = requestQueue;
        }

        #region UpdateViewDef

        /// <summary>
        /// Enqueues a request to the <c>UpdateViewDef</c> web service method through the agent.
        /// </summary>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid UpdateViewDef(Guid viewDefId, StringPair[] fields)
        {
            return UpdateViewDef(viewDefId, fields, GetUpdateViewDefDefaultBehavior());
        }

        /// <summary>
        /// Enqueues a request to the <c>UpdateViewDef</c> web service method through the agent.
        /// </summary>
        /// <param name="behavior">The behavior associated with the offline request being enqueued.</param>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid UpdateViewDef(Guid viewDefId, StringPair[] fields, OfflineBehavior behavior)
        {
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnUpdateViewDefReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnUpdateViewDefException");

            return EnqueueRequest("UpdateViewDef", behavior, viewDefId, fields);
        }

        public static OfflineBehavior GetUpdateViewDefDefaultBehavior()
        {
            OfflineBehavior behavior = GetAgentDefaultBehavior();
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnUpdateViewDefReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnUpdateViewDefException");

            return behavior;
        }

        #endregion UpdateViewDef

        #region UpdateEntityDef

        /// <summary>
        /// Enqueues a request to the <c>UpdateEntityDef</c> web service method through the agent.
        /// </summary>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid UpdateEntityDef(Guid entityDefId, EntityField[] fields)
        {
            return UpdateEntityDef(entityDefId, fields, GetUpdateEntityDefDefaultBehavior());
        }

        /// <summary>
        /// Enqueues a request to the <c>UpdateEntityDef</c> web service method through the agent.
        /// </summary>
        /// <param name="behavior">The behavior associated with the offline request being enqueued.</param>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid UpdateEntityDef(Guid entityDefId, EntityField[] fields, OfflineBehavior behavior)
        {
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnUpdateEntityDefReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnUpdateEntityDefException");

            return EnqueueRequest("UpdateEntityDef", behavior, entityDefId, fields);
        }

        public static OfflineBehavior GetUpdateEntityDefDefaultBehavior()
        {
            OfflineBehavior behavior = GetAgentDefaultBehavior();
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnUpdateEntityDefReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnUpdateEntityDefException");

            return behavior;
        }

        #endregion UpdateEntityDef

        #region AddInstance

        /// <summary>
        /// Enqueues a request to the <c>AddInstance</c> web service method through the agent.
        /// </summary>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid AddInstance(String entityDefName, EntityField[] fields)
        {
            return AddInstance(entityDefName, fields, GetAddInstanceDefaultBehavior());
        }

        /// <summary>
        /// Enqueues a request to the <c>AddInstance</c> web service method through the agent.
        /// </summary>
        /// <param name="behavior">The behavior associated with the offline request being enqueued.</param>
        /// <returns>The unique identifier associated with the request that was enqueued.</returns>
        public Guid AddInstance(String entityDefName, EntityField[] fields, OfflineBehavior behavior)
        {
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnAddInstanceReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnAddInstanceException");

            return EnqueueRequest("AddInstance", behavior, entityDefName, fields);
        }

        public static OfflineBehavior GetAddInstanceDefaultBehavior()
        {
            OfflineBehavior behavior = GetAgentDefaultBehavior();
            behavior.ReturnCallback = new CommandCallback(typeof(Callback), "OnAddInstanceReturn");
            behavior.ExceptionCallback = new CommandCallback(typeof(Callback), "OnAddInstanceException");

            return behavior;
        }

        #endregion AddInstance

        #region Common

        public static OfflineBehavior GetAgentDefaultBehavior()
        {
            OfflineBehavior behavior = new OfflineBehavior();
            behavior.MaxRetries = 3;
            behavior.Stamps = 1;
            behavior.Expiration = DateTime.Now + new TimeSpan(1, 0, 0, 0);
            behavior.ProxyFactoryType = typeof(Microsoft.Practices.SmartClient.DisconnectedAgent.WCFProxyFactory<Shp.Runtime.Contract.IDatamodelSC>);

            return behavior;
        }

        private Guid EnqueueRequest(string methodName, OfflineBehavior behavior, params object[] arguments)
        {
            Request request = CreateRequest(methodName, behavior, arguments);
            
            //Added Tag property to identify messages in Queue 
            request.Behavior.Tag = methodName;

            requestQueue.Enqueue(request);

            return request.RequestId;
        }

        private static Request CreateRequest(string methodName, OfflineBehavior behavior, params object[] arguments)
        {
            Request request = new Request();
            request.MethodName = methodName;
            request.Behavior = behavior;
            request.CallParameters = arguments;

            request.OnlineProxyType = OnlineProxyType;
            request.Endpoint = Endpoint;

            return request;
        }

        public static Type OnlineProxyType
        {
            get
            {
                return typeof(LitwareHR.PublicApp.WinUX.ServiceProxy.Secure.DataModelService);
            }
        }

        public static string Endpoint
        {
            get
            {
                return String.Empty;
            }
        }
        #endregion
    }
}
